## This script replicates all Tables and Figures in:

## Hamel, Brian T., and Derek E. Holliday. “Unequal Responsiveness in City Service Delivery: Evidence from 42 Million 311 Calls.” Quarterly Journal of Political Science

## First, load necessary packages:
install.packages(c("tidyverse", "patchwork", "ggrepel", "fixest", "scales"))
library(tidyverse)
library(patchwork)
library(ggrepel)
library(fixest)
library(scales)

## SET WORKING DIRECTORY HERE
#setwd()

## Loading data
load("dta.RData")
load("demand.RData")
load("demand_yearly.RData")
load("demand_common_services.RData")
load("tiles.RData")

#####################################
## Tables/Figures in the main text ##
#####################################

## Descriptive findings in text and footnotes
source("intext_results.R", print.eval = T)

## Table 1
source("Table1.R")
print(city_summ)

## Figure 1
source("Figure1.R")
ggsave("Figure1A.png", race_call_percap_c, height = 9, width = 5, units = "in", dpi = 600)
ggsave("Figure1B.png", inc_call_percap_c, height = 9, width = 5, units = "in", dpi = 600)

## Figure 2
source("Figure2.R")
ggsave("Figure2A.png", race_demand, height = 9, width = 5, units = "in", dpi = 600)
ggsave("Figure2B.png", poor_demand, height = 9, width = 5, units = "in", dpi = 600)

## Figure 3
source("Figure3.R")
ggsave("Figure3A.png", race_coefs, width =3, height = 3, units = "in", dpi = 700)
ggsave("Figure3B.png", inc_coefs, width =3, height = 3, units = "in", dpi = 700)


## Figure 4
source("Figure4.R")
ggsave("Figure4A.png", race_coefs, width =3, height = 3, units = "in", dpi = 700)
ggsave("Figure4B.png", inc_coefs, width =3, height = 3, units = "in", dpi = 700)


## Figure 5
source("Figure5.R")
ggsave("Figure5A.png", sf_street_income, height = 4, width = 4, units = "in", dpi = 600)
ggsave("Figure5B.png", austin_graffiti_income, height = 4, width = 4, units = "in", dpi = 600)


## Table 2
source("Table2.R")
print(Table2)

## Figure 6
source("Figure6.R")
ggsave("Figure6.png", graffiti_common, height = 4, width = 4, units = "in", dpi = 600)


## Table 3
source("Table3.R")
print(Table3)

##################################################
## Tables/Figures in the Supplementary Appendix ## 
##################################################

## Figure A1
source("FigureA1.R")
ggsave("FigureA1.png", clustering_hour,
       height = 11, width = 5, units = "in", dpi = 600)


## Figure A2
source("FigureA2.R")
ggsave("FigureA2.png", clustering_days_of_week,
       height = 11, width = 5, units = "in", dpi = 600)

## Figure A3
source("FigureA3.R")
ggsave("FigureA3.png", clustering_days_of_month,
       height = 11, width = 5, units = "in", dpi = 600)

## Figure A4
source("FigureA4.R")
ggsave("FigureA4.png", clustering_hour_white,
       height = 4, width = 6, units = "in", dpi = 600)

## Figure A5
source("FigureA5.R")
ggsave("FigureA5.png", clustering_hour_inc,
       height = 4, width = 6, units = "in", dpi = 600)

## Figure A6
source("FigureA6.R")
ggsave("FigureA6.png", clustering_days_of_week_white,
       height = 4, width = 6, units = "in", dpi = 600)

## Figure A7
source("FigureA7.R")
ggsave("FigureA7.png", clustering_days_of_week_inc,
       height = 4, width = 6, units = "in", dpi = 600)

## Figure A8
source("FigureA8.R")
ggsave("FigureA8.png", clustering_days_of_month_white,
       height = 6, width = 12, units = "in", dpi = 600)

## Figure A9
source("FigureA9.R")
ggsave("FigureA9.png", clustering_days_of_month_inc,
       height = 6, width = 12, units = "in", dpi = 600)

## Table A1
source("TableA1.R")
print(TableA1)

## Table A2
source("TableA2.R")
print(TableA2)

## Table A3
source("TableA3.R")
print(TableA3)

## Table A4
source("TableA4.R")
print(TableA4)

## Table A5
source("TableA5.R")
print(TableA5)

## Table A6
source("TableA6.R")
print(TableA6)

## NOTE: in Figs A10-11, to modify and examine individual cities, we recommend not using source()

## Figure A10
source("FigureA10.R")
ggsave("FigureA10A_Race.png", race_coefs_Austin)
ggsave("FigureA10A_Inc.png",  inc_coefs_Austin)
ggsave("FigureA10B_Race.png", race_coefs_Baltimore)
ggsave("FigureA10B_Inc.png",  inc_coefs_Baltimore)
ggsave("FigureA10C_Race.png", race_coefs_Boston)
ggsave("FigureA10C_Inc.png",  inc_coefs_Boston)
ggsave("FigureA10D_Race.png", race_coefs_Dallas)
ggsave("FigureA10D_Inc.png",  inc_coefs_Dallas)
ggsave("FigureA10E_Race.png", race_coefs_Denver)
ggsave("FigureA10E_Inc.png",  inc_coefs_Denver)
ggsave("FigureA10F_Race.png", race_coefs_Houston)
ggsave("FigureA10F_Inc.png",  inc_coefs_Houston)
ggsave("FigureA10G_Race.png", race_coefs_LA)
ggsave("FigureA10G_Inc.png",  inc_coefs_LA)
ggsave("FigureA10H_Race.png", race_coefs_Memphis)
ggsave("FigureA10H_Inc.png",  inc_coefs_Memphis)
ggsave("FigureA10I_Race.png", race_coefs_Nashville)
ggsave("FigureA10I_Inc.png",  inc_coefs_Nashville)
ggsave("FigureA10J_Race.png", race_coefs_NYC)
ggsave("FigureA10J_Inc.png",  inc_coefs_NYC)
ggsave("FigureA10K_Race.png", race_coefs_Philly)
ggsave("FigureA10K_Inc.png",  inc_coefs_Philly)
ggsave("FigureA10L_Race.png", race_coefs_SF)
ggsave("FigureA10L_Inc.png",  inc_coefs_SF)
ggsave("FigureA10M_Race.png", race_coefs_DC)
ggsave("FigureA10M_Inc.png",  inc_coefs_DC)

## Figure A11
source("FigureA11.R")
ggsave("FigureA11A_Race.png", race_coefs_Baltimore)
ggsave("FigureA11A_Inc.png",  inc_coefs_Baltimore)
ggsave("FigureA11B_Race.png", race_coefs_Boston)
ggsave("FigureA11B_Inc.png",  inc_coefs_Boston)
ggsave("FigureA11C_Race.png", race_coefs_Dallas)
ggsave("FigureA11C_Inc.png",  inc_coefs_Dallas)
ggsave("FigureA11D_Race.png", race_coefs_Houston)
ggsave("FigureA11D_Inc.png",  inc_coefs_Houston)
ggsave("FigureA11E_Race.png", race_coefs_Philly)
ggsave("FigureA11E_Inc.png",  inc_coefs_Philly)
ggsave("FigureA11F_Race.png", race_coefs_DC)
ggsave("FigureA11F_Inc.png",  inc_coefs_DC)

## Table A7
source("TableA7.R")
print(TableA7)

## Table A8
source("TableA8.R")
print(TableA8)

## Table A9
source("TableA9.R")
print(TableA9)

## Table A10
source("TableA10.R")
print(TableA10)

## Table A11
source("TableA11.R")
print(TableA11)

## Table A12
source("TableA12.R")
print(TableA12)

## Table A13
source("TableA13.R")
print(TableA13)
